package site.suremotoo.demo.delaypay.producer;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import site.suremotoo.demo.delaypay.config.QueueConstant;

/**
 * @author liulx
 * @description TODO
 * @date 2020-12-28 20:58
 */
@Component
public class MqProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 发送订单消息
     *
     * @param message 需要下单的数据
     * @return true 发送成功 false 发送失败
     */
    public boolean sendOrderMsg(String message) {
        System.out.println("发送订单消息：" + message);
        CorrelationData correlationData = new CorrelationData();
        rabbitTemplate.convertAndSend(QueueConstant.ORDER_EXCHANGE_NAME, QueueConstant.ORDER_ROUTING_KEY, message, correlationData);
        try {
            CorrelationData.Confirm confirm = correlationData.getFuture().get();
            return confirm.isAck();
        } catch (Exception e) {
            return false;
        }
    }

    public void sendPayMsg(String message) {
        rabbitTemplate.convertAndSend(QueueConstant.PAY_EXCHANGE_NAME, QueueConstant.PAY_ROUTING_KEY, message);
    }
}
